package com.cash.app.ui.other.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import coil3.load
import coil3.request.placeholder
import coil3.request.transformations
import coil3.transform.CircleCropTransformation
import com.cash.app.R
import com.cash.app.databinding.AcFeedbackListItemBinding
import com.cash.app.ui.other.model.FeedbackItem
import com.cash.app.utils.addHttpPrefix
import com.cash.app.utils.hide
import com.cash.app.utils.visible

class FeedbackListAdapter :
    ListAdapter<FeedbackItem, FeedbackListAdapter.ViewHolder>(DIFF_CALLBACK) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = AcFeedbackListItemBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    class ViewHolder(
        private val binding: AcFeedbackListItemBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(item: FeedbackItem) {
            binding.apply {
                if (item.userPic.isNotEmpty()) {
                    ivUserAvatar.load(item.userPic) {
                        transformations(CircleCropTransformation())
                        placeholder(R.mipmap.momo)
                    }
                }
                tvUserPhone.text = item.phone

                tvFeedbackContent.text = item.comment
                tvFeedbackTime.text = item.commentTimeString

                // Handle photos (max 3)
                val photos = item.photo
                if (photos.isEmpty()) {
                    llImageContainer.hide()
                    line2.hide()
                } else {
                    llImageContainer.visible()
                    line2.visible()
                    photos.getOrNull(0)?.let {
                        ivFeedback1.visible()
                        ivFeedback1.load(it.addHttpPrefix()) {
                            placeholder(R.mipmap.app_icon)
                        }
                    }
                    photos.getOrNull(1)?.let {
                        ivFeedback2.visible()
                        ivFeedback2.load(it.addHttpPrefix()) {
                            placeholder(R.mipmap.app_icon)
                        }
                    }
                    photos.getOrNull(2)?.let {
                        ivFeedback2.visible()
                        ivFeedback2.load(it.addHttpPrefix()) {
                            placeholder(R.mipmap.app_icon)
                        }
                    }
                }

                val hasReply = item.reply.isNotEmpty()
                if (hasReply) {
                    tvReplyContent.visible()
                    tvReplyTime.visible()
                    line3.visible()
                    tvReplyContent.text = item.reply
                    tvReplyTime.text = item.replyTimeString
                } else {
                    tvReplyContent.hide()
                    tvReplyTime.hide()
                    line3.hide()
                }
            }
        }
    }

    companion object {
        private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<FeedbackItem>() {
            override fun areItemsTheSame(oldItem: FeedbackItem, newItem: FeedbackItem): Boolean {
                return oldItem.comment == newItem.comment && oldItem.commentTimeString == newItem.commentTimeString
            }

            override fun areContentsTheSame(oldItem: FeedbackItem, newItem: FeedbackItem): Boolean {
                return oldItem == newItem
            }
        }
    }
} 